package com.yanqu.road.server.manger;

import com.yanqu.road.entity.activity.shushan.config.ShuShanActivityConfig;
import com.yanqu.road.logic.bussiness.config.LanguageBussiness;
import com.yanqu.road.entity.language.LanguageType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MultipleLanguageMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(MultipleLanguageMgr.class.getName());

    private static Map<String, Map<String, String>> languageMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean reloadConfig() {
        languageMap = LanguageBussiness.getMultipleLanguageMap();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    public static String getContent(String key, String language) {
        if (languageMap == null) {
            logger.info("languageMap is null, key:{}, languageType:{}", key, language);
            return key;
        }
        if (!languageMap.containsKey(key)) {
//            logger.info("not this key,  key:{}, languageType:{}.", key, language);
            return key;
        }
        if (!languageMap.get(key).containsKey(language)) {
//            logger.info("not this languageType,  key:{}, languageType:{}.", key, language);
            if(null != languageMap.get(key).get(LanguageType.LANGUAGE_DEFAULT)){
                return languageMap.get(key).get(LanguageType.LANGUAGE_DEFAULT);
            }else {
                return key;
            }
        }

        if (languageMap.get(key).get(language) == null) {
            logger.info("text is null,  key:{}, languageType:{}.", key, language);
            return key;
        }

        return languageMap.get(key).get(language);
    }


    /**
     * 获取蜀山所有随机宗派名字
     * @param language
     * @return
     */
    public static String randomShuShanName(String language){
        List<String> list = new ArrayList<>();
        for (Map.Entry<String, Map<String, String>> entry : languageMap.entrySet()){
            if(entry.getKey().contains(ShuShanActivityConfig.getSHUSHAN_SECT_NAME())){
                String name = entry.getValue().get(language);
                if(!StringUtils.isNullOrEmpty(name)){
                    list.add(name);
                }
            }
        }
        if(list.size() > 0){
            return list.get(new RandomHelper().next(list.size()));
        }
        return "";
    }

}
